package com.neusoft.a312cxy.android18cxy.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.a312cxy.android18cxy.R;
import com.neusoft.a312cxy.android18cxy.adapter.ClubcourseAdapter;
import com.neusoft.a312cxy.android18cxy.bean.ClubCourseBean;
import com.neusoft.a312cxy.android18cxy.bean.ClubInfo;
import com.neusoft.a312cxy.android18cxy.model.ClubModel;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Young on 2017/10/11.
 */

public class ClubInfoActivity extends AppCompatActivity {
    SharedPreferences sid;

    private TextView club_name;
    private TextView club_tel;
    private TextView introduction;
    private List<ClubInfo> list;
    private RecyclerView recyclerView;
    private ImageView clubthumb;
   // private VideoView videoView;
    private ClubInfo Club;
    private ImageView CollectButton;
  //  private Context mContext;
    private int id;
    private String userid,fans_id,club_type;
    private List<ClubInfo> mDaaList;
    private  String R_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_info);
        initView();
        saveDate();
        IsCollection();
        GetClubInfo();
        getClubCourse();
        CollectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collect();

            }
        });



    }

public void initView(){
    club_name=(TextView)findViewById(R.id.club_name);
    club_tel=(TextView)findViewById(R.id.club_tel);
    recyclerView=(RecyclerView)findViewById(R.id.ac);
    introduction=(TextView)findViewById(R.id.introduction) ;
    clubthumb=(ImageView)findViewById(R.id.clubthumb);
 //   videoView=(VideoView)findViewById(R.id.videoView);
    recyclerView.setLayoutManager(new LinearLayoutManager(ClubInfoActivity.this));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    CollectButton=(ImageView) findViewById(R.id.foodinfo_collect) ;


}

    public void saveDate(){
        Intent intent = getIntent();
        Bundle  bundle =  intent.getExtras();
        id = bundle.getInt("club_id");
        userid=bundle.getString("userid");
       // club_id = intent.getStringExtra("club_id");
        //club_type = intent.getStringExtra("club_type");
      //  sid=getSharedPreferences("userinfo", Context.MODE_PRIVATE);
     //   user_id=sid.getString("user_id", "");
    }


    public void GetClubInfo(){
        final ClubModel ClubModel=new ClubModel();
        Call<ClubInfo> call=ClubModel.getClubInfo(id);
        call.enqueue(new Callback<ClubInfo>() {
            @Override
            public void onResponse(Call<ClubInfo> call, Response<ClubInfo> response) {
                Club=response.body();
                if(!Club.getThumb().isEmpty())
                {
                    Picasso.with(ClubInfoActivity.this).load("http://172.24.10.175/workout/Uploads/"+Club.getThumb()).into(clubthumb);
                }
/*              else
                {
                    ClubInfoActivity.clubthumb.setImageResource(R.drawable.ic_club);
                }*/

/*                if(!Club.getClubthumb().isEmpty())
                {
                    Picasso.with(ClubInfoActivity.this).load("http://172.24.10.175/workout/Uploads/"+Club.getClubthumb()).into(clubthumb);
                }*/
/*                else{
                   ImageView.clubthumb.setImageResource(R.drawable.ic_club);
                }*/
                club_name.setText(Club.getClub_name());
                club_tel.setText(Club.getClub_tel());
                introduction.setText(Club.getIntroduction());

            }
/*            @Override
            public void onResponse(Call<List<ClubInfo>> call, Response<List<ClubInfo>> response) {
                List<ClubInfo> data=response.body();
                recyclerView.setAdapter(new ClubcourseAdapter(ClubInfoActivity.this,data));
            }*/
            @Override
            public void onFailure(Call<ClubInfo> call, Throwable t) {

            }
        });

    }

    public void getClubCourse() {

        ClubModel clubModel = new ClubModel();
        Call<ClubCourseBean> call = clubModel.getClubCourse(id);
        call.enqueue(new Callback<ClubCourseBean>() {
            @Override
            public void onResponse(Call<ClubCourseBean> call, Response<ClubCourseBean> response) {
                List<ClubCourseBean.ClubcourseBean> data = response.body().getClubcourse();
                //  System.out.println("9");
                recyclerView.setAdapter(new ClubcourseAdapter(ClubInfoActivity.this, data));

            }

            @Override
            public void onFailure(Call<ClubCourseBean> call, Throwable t) {

                //  System.out.println("8");
            }
        });
    }

    public void IsCollection(){
        ClubModel clubModel=new ClubModel();
        Call<String> call=clubModel.IsCollection("6","2","1");
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
             //   String result=response.body();
                R_result=response.body();
                if (R_result.equals("1")) {
                    CollectButton.setImageResource(R.drawable.ic_foodinfo_collect_nomal);
                } else {
                    CollectButton.setImageResource(R.drawable.ic_foodinfo_collect_press);
                }

            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });

    }
    public void collect(){
        ClubModel clubModel=new ClubModel();
        Call<String> call=clubModel.guanzhu("2","6","1");
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
           //     String collect=response.body();
                String result=response.body().trim();
               // R_result=response.body();
                if(result.equals("0")){
                    Toast.makeText(ClubInfoActivity.this, "关注失败", Toast.LENGTH_SHORT).show();
                    CollectButton.setImageResource(R.drawable.ic_foodinfo_collect_press);

                }
                else{
                    if(R_result.equals("0")){

                        CollectButton.setImageResource(R.drawable.ic_foodinfo_collect_nomal);
                        Toast.makeText(ClubInfoActivity.this, "取消关注成功", Toast.LENGTH_SHORT).show();
                        R_result = "{\"collected\":\"0\"}";
                }
                else
                    {


                        CollectButton.setImageResource(R.drawable.ic_foodinfo_collect_press);
                        Toast.makeText(ClubInfoActivity.this, "您已经关注成功", Toast.LENGTH_SHORT).show();
                        R_result = "{\"collected\":\"1\"}";

                }

                }

            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }


    }





